package com.heima.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dto.PageRequestDto;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.media.entity.WmSensitive;
import com.heima.media.mapper.WmSensitiveMapper;
import com.heima.media.service.IWmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author GQ
 * @since 2023-07-09
 */
@Service
@Slf4j
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements IWmSensitiveService {
    /**
     * 敏感词列表
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult<List> ListByPage(PageRequestDto dto) {
        Page<WmSensitive> ipage = new Page<>(dto.getPage(), dto.getSize());
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<WmSensitive> page = this.page(ipage);
        ResponseResult<List> result = new PageResponseResult<>(dto.getPage(), dto.getSize(), ipage.getTotal(), ipage.getRecords());
        return result;
    }

    /**
     * 新增敏感词
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult saveSensitive(WmSensitive wmSensitive) {
        LambdaQueryWrapper<WmSensitive> wrapper = new LambdaQueryWrapper();
        wrapper.eq(WmSensitive::getSensitives, wmSensitive.getSensitives());
        WmSensitive one = this.getOne(wrapper);
        if (one != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        WmSensitive sensitive = new WmSensitive();
        sensitive.setSensitives(wmSensitive.getSensitives());
        sensitive.setCreatedTime(new Date());
        boolean save = this.save(sensitive);
        if (save) {
            return ResponseResult.okResult(sensitive);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    /**
     * 删除敏感词
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteSensitive(Integer id) {
        boolean b = this.removeById(id);
        if (b) {
            return ResponseResult.okResult();
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
    }

    /**
     * 敏感词修改
     *
     * @param wmSensitive
     * @return
     */
    @Override
    public ResponseResult updateSensitive(WmSensitive wmSensitive) {
        LambdaUpdateWrapper<WmSensitive> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(WmSensitive::getId, wmSensitive.getId());
        wrapper.set(WmSensitive::getSensitives, wmSensitive.getSensitives());
        boolean update = this.update(wrapper);
        if (update) {
            return ResponseResult.okResult(wmSensitive);
        } else {
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }
}
